package com.example.fuluchou.service.activitystatus.operator;

import com.example.fuluchou.dao.dataobject.ActivityUserDO;
import com.example.fuluchou.dao.mapper.ActivityUserMapper;
import com.example.fuluchou.service.dto.ConvertActivityStatusDTO;
import com.example.fuluchou.service.enums.ActivityUserStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 用户模块
 */
@Component
public class UserOperator extends AbstractActivityOperator{
    @Override
    public Integer sequence() {
        return 1;
    }
    @Autowired
    private ActivityUserMapper activityUserMapper;

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActicityId();
        List<Long> userIds = convertActivityStatusDTO.getUserIds();
        ActivityUserStatusEnum targetUserStatus = convertActivityStatusDTO.getTargetUserStatus();
        if(null == activityId
                || null == userIds
                || null == targetUserStatus) {
            return false;
        }

        List<ActivityUserDO> activityUserDOList =
                activityUserMapper.batchSelectByAUIds(activityId,userIds);
        if(CollectionUtils.isEmpty(activityUserDOList)){
            return false;
        }
        for(ActivityUserDO userDO : activityUserDOList){
            if(userDO.getStatus()
                    .equalsIgnoreCase(targetUserStatus.name())) {
                return false;
            }
        }

        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActicityId();
        List<Long> userIds = convertActivityStatusDTO.getUserIds();
        ActivityUserStatusEnum targetUserStatus = convertActivityStatusDTO.getTargetUserStatus();

        try {
            activityUserMapper.batchUpdateStatus(activityId,userIds,targetUserStatus.name());
            return true;
        }catch (Exception e){
            return false;
        }

    }
}
